﻿using System.Windows.Forms;
using HBFFirmwareGetter.Code;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace HBFFirmwareGetter
{
	/// <summary>
	/// Represents Result form class.
	/// </summary>
	public partial class Results : Form
	{
		#region Constructor

		/// <summary>
		/// Initializes a new instance of the <see cref="Results"/> class.
		/// </summary>
		/// <param name="resultCollection">The result collection.</param>
		public Results(List<string> resultCollection)
		{
			InitializeComponent();

			lbResults.Items.AddRange(resultCollection.ToArray());
		}

		#endregion Constructor

		#region Handlers

		#region Button Handlers

		/// <summary>
		/// Handles the Click event of the btnClose control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void btnClose_Click(object sender, EventArgs e)
		{
			// Clear counter
			Core.FilesCounter = 0;

			// Clear list box items
			lbResults.Items.Clear();

			this.Close();
		}

		/// <summary>
		/// Handles the Click event of the btnSave control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void btnSave_Click(object sender, EventArgs e)
		{
			saveDialog.AddExtension = true;
			saveDialog.RestoreDirectory = true;
			saveDialog.FileName = "Firmware";
			saveDialog.Filter = "Text files (*.txt)|*.txt";
			saveDialog.DefaultExt = "txt";

			if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				SaveFile(saveDialog.FileName);
			}
		}

		#endregion Button Handlers

		#endregion Handlers

		#region Internal Implementation

		/// <summary>
		/// Saves the file.
		/// </summary>
		/// <param name="path">The path to save.</param>
		private void SaveFile(string path)
		{
			TextWriter resultFile = new StreamWriter(path, true, Encoding.ASCII);

			foreach (var item in lbResults.Items)
			{
				resultFile.WriteLine(item);
			}

			resultFile.Flush();
			resultFile.Close();
		}

		#endregion Internal Implementation
	}
}
